package com.hq.petmap;

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;

import android.content.Context;

public class FileStorage {

	private File fileDir;

	public FileStorage(Context context) {
		fileDir = context.getCacheDir();
		if (!fileDir.exists())
			fileDir.mkdirs();
	}

	public File getFile(String filePath) {
		return new File(fileDir, filePath);
	}

	public File saveFile(File file, InputStream inputStream) {
		try {
			if (file == null) {
				return null;
			}
			OutputStream os = new FileOutputStream(file);
			int bytesRead = 0;
			byte[] buffer = new byte[1024];
			while ((bytesRead = inputStream.read(buffer, 0, 1024)) != -1) {
				os.write(buffer, 0, bytesRead);
			}
			os.close();
			return file;
		} catch (Exception ex) {
			ex.printStackTrace();
			return null;
		}
	}
}
